/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package wisimconsole;

import java.awt.Color;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JTextPane;
import javax.swing.JScrollPane;
import javax.swing.text.StyleConstants;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;

/**
 *
 * @author brother
 */
public class TestJTextPane {

    public static void main(String[] args) throws BadLocationException {
	String[] value=new String[]{"true","true","10000"};
	args=value;
	if (args.length != 3) {
	    System.err.println("Please give 3 arguments:");
	    System.err.println(" [true/false] for use batch "
		    + "(true) vs. use default doc [false]");
	    System.err.println(
		    " [true/false] for update while visible");
	    System.err.println(
		    " [int] for number of strings to insert");
	    System.exit(-1);
	}

	boolean useBatch = args[0].toLowerCase().equals("true");
	boolean updateWhileVisible = args[1].equals("true");
	int iterations = Integer.parseInt(args[2]);
	System.out.println("Using batch = " + useBatch);
	System.out.println("Updating while pane visible = "
		+ updateWhileVisible);
	System.out.println("Strings to insert = " + iterations);

	JFrame f = new JFrame("Document Speed Test");
	f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	f.getContentPane().setLayout(new BorderLayout());
	JTextPane jtp = new JTextPane();
	f.getContentPane().add( new JScrollPane(jtp), BorderLayout.CENTER);
	f.setSize(400, 400);
	f.show();

	// Make one of each kind of document.
	BatchDocument bDoc = new BatchDocument();
	DefaultStyledDocument doc = new DefaultStyledDocument();
	if (updateWhileVisible) {
	    if (useBatch) {
		jtp.setDocument(bDoc);
	    } else {
		jtp.setDocument(doc);
	    }
	}
	long start = System.currentTimeMillis();

	// Make some test data. Normally the text pane
	// content would come from other source, be parsed, and
	// have styles applied based on appropriate application
	// criteria. Here we are interested in the speed of updating
	// a document, rather than parsing, so we pre-parse the data.
	String[] str = new String[]{ "The ", "quick ", "brown ", "fox ", "jumps ", "over ", "the ", "lazy ", "dog. "};
	Color[] colors = new Color[]{Color.red, Color.blue, Color.green};
	int[] sizes = new int[]{10, 14, 12, 9, 16};

	// Add the test repeatedly
	int offs = 0;
	int count = 0;
	SimpleAttributeSet attrs = new SimpleAttributeSet();
	for (int i = 0; i < iterations; i++) {
	    for (int j = 0; j < str.length; j++) {
		// Make some random style changes
		StyleConstants.setFontSize( attrs, sizes[count % sizes.length]);
		StyleConstants.setForeground( attrs, colors[count % colors.length]);

		if (useBatch) {
		    bDoc.appendBatchString(str[j], attrs);
		} else {
		    doc.insertString(offs, str[j], attrs);
		}

		// Update out counters
		count++;
		offs += str[j].length();
	    }

	    // Add a linefeed after each instance of the string
	    if (useBatch) {
		bDoc.appendBatchLineFeed(attrs);
	    } else {
		doc.insertString(offs, "\n", attrs);
	    }
	    offs++;
	}

	// If we're testing the batch document, process all
	// of the updates now.
	if (useBatch) {
	    bDoc.processBatchUpdates(0);
	}

	System.out.println("Time to update = " + (System.currentTimeMillis() - start));
	System.out.println("Text size = " + offs);

	if (!updateWhileVisible) {
	    if (useBatch) {
		jtp.setDocument(bDoc);
	    } else {
		jtp.setDocument(doc);
	    }
	}
    }
}
